using Mapster;
using SmartCommunity.Models;
using System.ComponentModel.DataAnnotations;

namespace SmartCommunity.Services.Community
{
    #region DTO属性映射
    /// <summary>
    /// 属性映射 RoomDtoRegister
    /// </summary>
    public class RoomDtoRegister : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<RoomEntity, RoomDto>()
                .Map(d => d.BuildingName, s => s.Building.BuildingName, s => s.Building != null)
                .Map(d => d.AreaName, s => s.Building.Area.AreaName, s => s.Building != null && s.Building.Area != null)
                .Map(d => d.CommunityName, s => s.Building.Area.CommunityName, s => s.Building != null && s.Building.Area != null)
                ;
        }
    }
    #endregion

    #region 数据传输对象 RoomDto
    /// <summary>
    /// 数据传输对象：
    /// </summary>
    public class RoomDto : BaseDto
    {
        /// <summary>
        /// 房间Id
        /// </summary>
        public Guid RoomId { get; set; }

        /// <summary>
        /// 楼栋ID
        /// </summary>
        public Guid BuildingId { get; set; }

        public string? BuildingName { get; set; }

        public string? AreaName { get; set; }

        public string? CommunityName { get; set; }


        /// <summary>
        /// 房号
        /// </summary>
        [Required(ErrorMessage = "房号不能为空")]
        [StringLength(20, ErrorMessage = "房号 长度不能超过 20.")]
        public string RoomNo { get; set; } = string.Empty;

        /// <summary>
        /// 完整房号
        /// </summary>
        public string RoomCode { get; set; } = string.Empty;

        /// <summary>
        /// 所在楼层
        /// </summary>
        public int RoomFloor { get; set; }

        /// <summary>
        /// 房间用途
        /// </summary>
        public int RoomUsage { get; set; }

        /// <summary>
        /// 户型
        /// </summary>
        public int RoomType { get; set; }

        /// <summary>
        /// 房间状态
        /// </summary>
        public int RoomState { get; set; }

        /// <summary>
        /// 装修状态
        /// </summary>
        public int RoomDeco { get; set; }

        public string? RoomStateName { get; set; }
        public string? RoomTypeName { get; set; }
        public string? RoomUsageName { get; set; }
        public string? RoomDecoName { get; set; }

        /// <summary>
        /// 建筑面积
        /// </summary>
        public decimal? BuildArea { get; set; }

        /// <summary>
        /// 使用面积
        /// </summary>
        public decimal? UsageArea { get; set; }

        /// <summary>
        /// 是否空置房
        /// </summary>
        public ulong? IsIdle { get; set; }

        /// <summary>
        /// 户主
        /// </summary>
        [StringLength(50, ErrorMessage = "户主 长度不能超过 50.")]
        public string? Manager { get; set; }

        /// <summary>
        /// 户主电话
        /// </summary>
        [StringLength(20, ErrorMessage = "户主电话 长度不能超过 20.")]
        public string? ManagerTel { get; set; }

        /// <summary>
        /// 备注说明
        /// </summary>
        [StringLength(255, ErrorMessage = "备注说明 长度不能超过 255.")]
        public string? Remark { get; set; }

    }
    #endregion

    #region 数据查询对象
    /// <summary>
    /// 查询模型：
    /// </summary>
    public class RoomQuery : AbstractQuery<RoomEntity>
    {
        /// <summary>
        /// 主键ID多选查询
        /// </summary>
        public IEnumerable<Guid> Ids { get; set; } = [];

        public Guid? BuildingId { get; set; }

        /// <summary>
        /// 可能是社区ID，也可能是小区id，或建筑ID
        /// </summary>
        public Guid? MBuildingId { get; set; }


        /// <summary>
        /// 根据查询条件添加查询表达式
        /// </summary>
        protected override void MergeQueryExpression()
        {
            AddExpression(Ids.Any(), e => Ids.Contains(e.RoomId));
            AddExpression(BuildingId.HasValue, a => a.BuildingId == BuildingId!.Value);
            if (MBuildingId.HasValue)
            {
                AddExpression(a => a.BuildingId == MBuildingId!.Value 
                || a.Building.AreaId == MBuildingId.Value 
                || a.Building.Area.CommunityId == MBuildingId.Value
                || a.Building.ParentId == MBuildingId.Value
                );
            }
             AddSearchExpression(s => o => o.RoomCode.Contains(s) || (o.Manager??"").Contains(s));
        }
    }
    #endregion
}
